﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.ViewFieldCollection", ServerTypeId="{af975f76-8a94-4e6d-8325-bd1e20b7c301}")]
    public class ViewFieldCollection : ClientObjectCollection<string>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public ViewFieldCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public void Add(string strField)
        {
            if (base.Context.ValidateOnClient && (strField == null))
            {
                throw ClientUtility.CreateArgumentNullException("strField");
            }
            ClientAction query = new ClientActionInvokeMethod(this, "Add", new object[] { strField });
            base.Context.AddQuery(query);
            this.OnAdd(strField);
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            string str;
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (!flag && (((str = peekedName) != null) && (str == "SchemaXml")))
            {
                flag = true;
                reader.ReadName();
                base.ObjectData.Properties["SchemaXml"] = reader.ReadString();
            }
            return flag;
        }

        [Remote]
        public void MoveFieldTo(string field, int index)
        {
            if (base.Context.ValidateOnClient)
            {
                if (field == null)
                {
                    throw ClientUtility.CreateArgumentNullException("field");
                }
                if (index < 0)
                {
                    throw ClientUtility.CreateArgumentException("index");
                }
            }
            ClientAction query = new ClientActionInvokeMethod(this, "MoveFieldTo", new object[] { field, index });
            base.Context.AddQuery(query);
        }

        private void OnAdd(string fieldName)
        {
            base.Data.Add(fieldName);
        }

        private void OnRemove(string fieldName)
        {
            base.Data.Remove(fieldName);
        }

        private void OnRemoveAll()
        {
            List<object> data = base.Data;
            while (data.Count > 0)
            {
                data.RemoveAt(data.Count - 1);
            }
        }

        [Remote]
        public void Remove(string strField)
        {
            if (base.Context.ValidateOnClient && (strField == null))
            {
                throw ClientUtility.CreateArgumentNullException("strField");
            }
            ClientAction query = new ClientActionInvokeMethod(this, "Remove", new object[] { strField });
            base.Context.AddQuery(query);
            this.OnRemove(strField);
        }

        [Remote]
        public void RemoveAll()
        {
            ClientAction query = new ClientActionInvokeMethod(this, "RemoveAll", null);
            base.Context.AddQuery(query);
            this.OnRemoveAll();
        }

        [Remote]
        public string SchemaXml
        {
            get
            {
                base.CheckUninitializedProperty("SchemaXml");
                return (string) base.ObjectData.Properties["SchemaXml"];
            }
        }
    }
}

